<template>
  <el-dialog
    :title="optionType==='see'?'查看':(optionType==='edit'?'编辑':(optionType==='add'?'添加':'提示'))"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="close"
    width="80%"
  >
    <div>
      <el-form ref="form" class="add-edit-form" :model="formData" label-width="130px">
        <el-form-item label="角色名">
          <el-input
            v-model="formData.roleName"
            class="form-item-val"
            :disabled="optionType==='see' || (optionType==='edit' && false)"
          />
        </el-form-item>
        <el-form-item label="角色权限">
          <el-cascader
            v-model="formData.roleMenu"
            class="form-item-val"
            :options="menuTreeSelect"
            collapse-tags
            :props="{ multiple: true }"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <div v-if="optionType==='edit' || optionType==='add'">
        <el-button @click="close">取 消</el-button>
        <l-button auto-loading type="primary" @click="save">确 定</l-button>
      </div>
      <el-button v-else type="primary" @click="close">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getById, updateById, saveData } from '@/api/modules/system/sysRoleApi'
import { getTreeSelect, getTreeSelectValueByRoleId } from '@/api/modules/system/sysMenuApi'
import LButton from '@/components/loadingButton/LButton'

export default {
  name: 'Edit',
  components: {
    LButton
  },
  data() {
    return {
      optionType: '',
      dialogVisible: false,
      formData: {
        id: '',
        roleName: '',
        roleMenu: []
      },
      // 主键
      id: null,
      menuTreeSelect: []
      /* 字典表数据*/
    }
  },
  methods: {
    open(optionType, id) {
      this.optionType = optionType
      this.id = id
      this.dialogVisible = true
      if (optionType !== 'add') {
        this.getTreeSelectValueByRoleIdData(this.id)
        this.getByIdData()
      }
      this.getTreeSelectData()
    },
    close() {
      this.formData = {
        id: '',
        roleName: '',
        roleMenu: []
      }
      this.dialogVisible = false
    },
    save(next) {
      const roleMenu = new Set()
      for (const mList of this.formData.roleMenu) {
        for (const m of mList) {
          roleMenu.add(m)
        }
      }
      if (this.optionType === 'add') {
        saveData({
          roleName: this.formData.roleName,
          menuIdSet: Array.from(roleMenu)
        }).then(res => {
          this.$message.success('添加成功')
          this.close()
          this.$emit('refreshPageData')
        }).finally(() => {
          next()
        })
      } else if (this.optionType === 'edit') {
        updateById({
          id: this.formData.id,
          roleName: this.formData.roleName,
          menuIdSet: Array.from(roleMenu)
        }).then(res => {
          this.$message.success('修改成功')
          this.close()
          this.$emit('refreshPageData')
        }).finally(() => {
          next()
        })
      }
    },
    getByIdData() {
      getById(this.id).then(res => {
        this.formData.id = res.obj.id
        this.formData.roleName = res.obj.roleName
      })
    },
    getTreeSelectData() {
      getTreeSelect().then(res => {
        this.menuTreeSelect = res.rows
      })
    },
    getTreeSelectValueByRoleIdData(roleId) {
      getTreeSelectValueByRoleId({
        roleId: roleId
      }).then(res => {
        this.formData.roleMenu = res.rows
      })
    }
  }
}
</script>

<style scoped>

</style>
